package com.atguigu.gmall.cart.controller;

import com.atguigu.gmall.cart.model.CartInfo;
import com.atguigu.gmall.cart.service.CartService;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.util.AuthContextHolder;
import com.sun.org.apache.regexp.internal.RE;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author: atguigu
 * @create: 2023-03-08 10:39
 */
@RestController
@RequestMapping("/api/cart")
public class CartController {

    @Autowired
    private CartService cartService;


    /**
     * 将商品加入购物车
     *
     * @param skuId
     * @param skuNum
     * @return
     */
    @GetMapping("/addToCart/{skuId}/{skuNum}")
    public Result addToCart(HttpServletRequest request, @PathVariable("skuId") Long skuId, @PathVariable("skuNum") Integer skuNum) {
        //1.获取临时用户ID  登录用户ID
        String userId = "";

        String userTempId = AuthContextHolder.getUserTempId(request);
        if (StringUtils.isNotBlank(userTempId)) {
            userId = userTempId;
        }
        String loginUserId = AuthContextHolder.getUserId(request);
        if (StringUtils.isNotBlank(loginUserId)) {
            userId = loginUserId;
        }
        //2.调用业务逻辑层完成加入购物车操作
        cartService.addToCart(userId, skuId, skuNum);
        return Result.ok();
    }


    /**
     * 获取用户购物车商品列表(包含购物车合并逻辑)
     *
     * @param request
     * @return
     */
    @GetMapping("/cartList")
    public Result cartList(HttpServletRequest request) {
        //1.获取用户ID
        String userId = AuthContextHolder.getUserId(request);
        //2.获取临时用户ID
        String userTempId = AuthContextHolder.getUserTempId(request);
        //3.调用业务合并购物车业务
        List<CartInfo> cartInfoList = cartService.cartList(userId, userTempId);
        return Result.ok(cartInfoList);
    }


    /**
     * 修改购物车商品选中状态
     *
     * @param skuId
     * @param isChecked
     * @return
     */
    @GetMapping("/checkCart/{skuId}/{isChecked}")
    public Result updateCheck(HttpServletRequest request, @PathVariable("skuId") Long skuId, @PathVariable("isChecked") int isChecked) {
        //1.获取临时用户ID  登录用户ID
        String userId = getUserId(request);
        cartService.updateCheck(userId, skuId, isChecked);
        return Result.ok();
    }

    /**
     * 尝试获取用户ID(登录用户ID或者临时用户ID)
     * @param request
     * @return
     */
    private String getUserId(HttpServletRequest request) {
        String userId = "";
        String userTempId = AuthContextHolder.getUserTempId(request);
        if (StringUtils.isNotBlank(userTempId)) {
            userId = userTempId;
        }
        String loginUserId = AuthContextHolder.getUserId(request);
        if (StringUtils.isNotBlank(loginUserId)) {
            userId = loginUserId;
        }
        return userId;
    }

    /**
     * 删除购物车中商品
     * @param skuId
     * @return
     */
    @DeleteMapping("/deleteCart/{skuId}")
    public Result deleteCart(HttpServletRequest request, @PathVariable("skuId") Long skuId){
        String userId = getUserId(request);
        cartService.deleteCart(userId, skuId);
        return Result.ok();
    }
}
